import {
  IsNotEmpty,
  IsMobilePhone,
  IsMongoId,
  Length,
  IsString,
  IsEmail,
  IsDateString,
  Min,
  Max,
  IsEnum,
  IsOptional
} from 'class-validator';
import { PartialType } from '@nestjs/mapped-types';
import { Transform } from 'class-transformer';

export class UserDto {
  @IsNotEmpty({ message: '账号不能为空' })
  @Length(2, 20, { message: '账号长度必须在2到20个字符之间' })
  account: string;

  @IsNotEmpty({ message: '密码不能为空' })
  @Transform(({ value }) => value.toString())
  @Length(6, 20, { message: '密码长度必须在6到20位之间' })
  password: string;

  @IsNotEmpty({ message: '手机号码不能为空' })
  @IsMobilePhone('zh-CN', {}, { message: '手机号码格式不正确' })
  mobile: string;

  @IsOptional()
  @Length(2, 10, { message: '姓名长度必须在2到10个字符之间' })
  name?: string;

  @IsOptional()
  @IsEnum(['MALE', 'FEMALE'], { message: '性别只能是男或女' })
  sex?: number;

  @IsOptional()
  @IsDateString({}, { message: '出生日期格式不正确' })
  birthday?: string;

  @IsOptional()
  @Min(0, { message: '年龄不能小于0岁' })
  @Max(120, { message: '年龄不能大于120岁' })
  age?: number;

  @IsOptional()
  @IsString()
  avatar?: string;

  @IsOptional()
  @IsEmail({}, { message: '邮箱格式不正确' })
  email?: string;

  @IsOptional()
  @Transform(({ value }) => value.toString())
  @IsEnum(['0', '1', '2', '3'], { message: '会员等级不正确' })
  member?: boolean;
}

export class UpdateUserDto extends PartialType(UserDto) {
  @IsMongoId({ message: '用户ID格式不正确' })
  id: string;
}

export class UserSearchDto extends PartialType(UserDto) {}

export class UserPageSearchDto extends PartialType(UserDto) {
  @IsNotEmpty({ message: '当前页码不能为空' })
  @Transform(({ value }) => Number(value))
  current: number;

  @IsNotEmpty({ message: '每页显示条数不能为空' })
  @Transform(({ value }) => Number(value))
  size: number;
}
